<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <%@ include file="/common/plateform/include/common-base.jsp"%>
  <title>选择数据表对象-表单设计器</title>
</head>

<style>

.content-wrapper {
  margin-left: 0px;
}
</style>
<body class="hold-transition ${sysColor} sidebar-mini">
<div class="wrapper">

  <!-- Main start -->
  <div class="content-wrapper">
    <!-- Main start -->
	<section class="content">
      <div class="row">
        <div class="col-xs-12 ibox">
        
          <!--搜索  start -->
          <div class="box box-info">
          
             <!--搜索title  start --> 
              
            <!--搜索title  end --> 
            <div class="box-body">
			  <form id="searchForm" class="form-horizontal" action="javascript:;">
				  <input type="hidden" name="pageName" value="objName-table"/>
				  <input type="hidden" name="formName" value="searchForm"/>
				  <input type="hidden" name="orderBy" value="sortInfo.info_table_name asc"/>
				<div class="form-group">
				
					<div class="col-sm-1  text-right"><label class="control-label" >中文名：</label></div>
					<div class="col-sm-2">
						 <input type="text" class="form-control clean"  name="sortInfo.wl_sortName" value=""/>
					</div>
					
					<div class="col-sm-1  text-right"><label class="control-label" >英文名：</label></div>
					<div class="col-sm-2">
						 <input type="text" class="form-control clean"  name="sortInfo.wl_infoTableName" value=""/>
					</div>
					
					<div class="col-sm-1  text-right"><label class="control-label" >对象名：</label></div>
					<div class="col-sm-2">
						 <input type="text" class="form-control clean"  name="sortInfo.wi_objName" value="${param.objNameStr}"/>
					</div>

					<div class="col-sm-1  text-right"><label class="control-label" >系统：</label></div>
					<div class="col-sm-2">
						<input type="text" class="form-control"  name="sortInfo.w_sysName" value="${param.sysEname}"/>
					</div>
				</div>
				  <div class="form-group">
					<div class="col-sm-12 text-center">
						 <button type="button" class="btn btn-success" onclick="getTableData();"><i class="fa fa-search"></i> 查询</button>&nbsp;&nbsp;
				      	 <button type="button" class="btn btn-warning" onclick="restSearchForm()">重置 </button>&nbsp;&nbsp;
				      	 <button type="button" class="btn btn-danger" onclick="checkedSaveOk()"><i class="fa fa-check-square-o"></i> 选择完成 </button>
					</div>
					
				</div>
				
			  </form>
            </div>
	     </div>	
	     <!-- 搜索 end -->
        
        <div class="box box-success">
            <!--列表title  start --> 
	        <div class="box-header with-border">
                <div id="checked_obj_div"></div>
            </div>
            
            <!-- /.box-header -->
            <div class="box-body">
              <table id="dataList" class="table table-bordered " >
                <thead class="bg-success">
                <tr>
            	  <th>中文表名</th>
                  <th>英文表名</th>
                  <th>数据对象名</th>
                  <th class="text-center">操作</th>
                </tr>
                </thead>
                <tbody id="dataList-body">
                </tbody>
              </table>
              <center>
    	  		<ul id="dataList-page"></ul>
              </center>
            </div>
            
          </div>
        </div>
      </div>
    </section>
    <!-- Main end -->
    
 <script type="text/template" id="dataList-template">
		<? for ( var i = 0; i < rows.length; i++) {?>
			<tr>
				<td><?=(pageObj.currentPage-1)*10+(i+1)?>.<?=strUtil.doNull(rows[i].sortInfo.sortName,'')?></td>
				<td><?=strUtil.doNull(rows[i].sortInfo.infoTableName,'')?></td>
				<td><?=strUtil.doNull(rows[i].sortInfo.objName,'')?></td>
				<td class="text-center">
					<input type="checkbox" text-name="<?=rows[i].sortInfo.sortName?>" sort-id="<?=rows[i].sortInfo.sortId?>" obj-name="<?=rows[i].sortInfo.objName?>" table-name="<?=rows[i].sortInfo.infoTableName?>" onclick="getChecked(this)"/>
				</td>
			</tr>
		<?}?>
</script>

<script>
var clickMenu = "objName-table";
var id,myPage,dataListId,searchFormId,isShowLog,objName,objId;
myPage = 20;
dataListId = "dataList";
searchFormId = "searchForm";
objName = "pubMetadata";
objId = "infoId";
isShowLog = true;
var id;
var objNameStr="${param.objNameStr}";
$(function () {
	//1、加载分页列表数据
	getTableData();
	//2、控制是否启用token
	//setToken();
	id = $('tr.selected th').eq(0).html();
	//请在下面补充其他初始化方法（如数据字典、页面加载数据等）
//	DictUtil.getDictExtList("#proUnit", dictUrl, {cateEname:"productUnit"});
//	DictUtil.getDataToSelect("#selectCate",{modelTypes:"s_trpCate"},{value:"cateId",text:"cateName",other:"cateNo,cateType"});
	
 });


function setInfoTableName(formId){
	var sysCode = $("#"+formId+" #tablePrefix").val();
	var sortCode = $("#"+formId+" #sortCode").val();
	var tableName = sysCode+"_"+sortCode;
	$("#"+formId+" #infoTableName").val(tableName);
	$("#"+formId+" #objName").val(sysCode+tranformStr1(sortCode));
}
//字符串转成驼峰的方法
function transformStr3(str){
    var re=/_(\w)/g;
    return str.replace(re,function ($0,$1){
        return $1.toUpperCase();
    });
}

//首字母大写
function tranformStr1(str){
    var strArr=transformStr3(str);
    strArr=strArr.charAt(0).toUpperCase()+strArr.substring(1);
    return strArr;
}
//重置查询，同时清空之前选中项和内容。
function restSearchForm(){
	mytools.cleanData("#searchForm");
	$("#checked_obj_div").html("");
	$("#"+dataListId+"-body").find("tr td").find("input[type='checkbox']").each(function(){
		$(this).prop("checked",false);
		$(this).parent().removeClass("bg-danger");
	});
}
$(function () {
	$('#'+dataListId+' tbody').on('click','tr', function (e) {
	     if ( $(this).hasClass('selected') ) {
	            //$(this).removeClass('selected');
	        }
	        else {
	            $('tr.selected').removeClass('selected');
	            $(this).addClass('selected');
	        }
	     id = $('tr.selected td').eq(0).html();
	   //  console.log($('tr.selected td').eq(1).html())
	} );
	
	
  });
//加载列表数据
function getTableData(){
	deleteAllRow(dataListId);
	$.ajax({
		   type:'get',
		   url:common_list_url,
		   data:$("#"+searchFormId).serialize()+getCurrentPage(common_list_url+"?"+$("#"+searchFormId).serialize(),myPage),
		   dataType:'json',
		   success: function(myData){
			var data = baidu.template(dataListId+"-template",eval(myData));  
			$("#"+dataListId+"-body").html(data);
			  var option= {
					   ele:$("#"+dataListId+"-page")[0],
					   total:myData.total,
					   fn:getTableData,
					   url:common_list_url+"?"+$("#"+searchFormId).serialize()
				   }
				generatePages(option);
			  
			  checkedObjTr();
		   }
	
		});	
  }



function checkedObjTr(){
	if(!strUtil.isNull(objNameStr)){
		var objNameArrays = objNameStr.split(",");
		var objNameMap = {};
		for(var i=0;i<objNameArrays.length;i++){
			var key = objNameArrays[i];
			objNameMap[key]=key;
		}
		$("#"+dataListId+"-body").find("tr td").find("input[type='checkbox']").each(function(){
			if(objNameMap[$(this).attr("obj-name")]){
				$(this).click();//触发checkbox的onclick点击事件
			}
		});
	}
}

function refresh(){
	getTableData();
}


function setToken(){
	$.ajax({
	    type:'get',
	    url:'${ctx}/sendPage/token?token=true',
	    dataType:'text',
	    success: function(myData){
	    	console.log(myData);
			 $('#formToken').val(myData);
	  	}
	});	
}

//勾选对象
function getChecked(obj){
	var textName = $(obj).attr("text-name");
	var objName = $(obj).attr("obj-name");
	var tableName = $(obj).attr("table-name");
	var sortId = $(obj).attr("sort-id");
    var objPkCoum = $(obj).attr("obj-pk-colum");

    var index_load = layer.load(1, {shade: false});
    var paramData = {};
    paramData["pageName"]="objName-table-column";
    paramData["formName"]="searchForm";
    paramData["metadata.wi_dataRestrict"]="1,4";
    paramData["metadata.w_sortId"]=sortId;
    $.getJSON(common_list_url,paramData,function(myData){
        console.log("metadata",myData);
        layer.close(index_load);
        if(myData.total>0){
            var primaryKeys = "";
			for(var i=0;i<myData.rows.length;i++){
			    var objs = myData.rows[i].metadata;
                primaryKeys+=objs.dataAttr+",";
			}
            if(primaryKeys.length>0){
                primaryKeys=primaryKeys.substring(0,primaryKeys.length-1);
			}
            $(obj).parent().addClass("bg-danger");
            if($(obj).is(':checked')){
                var divHtml = " <button type=\"button\" id='btn-"+objName+"' text-name=\""+textName+"\" obj-primary-keys=\""+primaryKeys+"\" obj-name=\""+objName+"\" table-name=\""+tableName+"\" obj-pk-colum=\""+objPkCoum+"\" class=\"btn btn-danger btn-sm\"> "+textName+"（"+objName+"-"+primaryKeys+"） </button> ";
                $("#checked_obj_div").append(divHtml);
            }else{
                $("#checked_obj_div").find("#btn-"+objName).remove();
                $(obj).parent().removeClass("bg-danger");
            }
        }else{
            layer.msg("该表无主键！");
        }
    });

}
//选择完成后，保存对象
function checkedSaveOk(){
	var btnMap = {};
	$("#checked_obj_div").find("button").each(function(){
		var tableTextName = $(this).attr("text-name");
		var tableName = $(this).attr("table-name");
		var tableObjName = $(this).attr("obj-name");
		var tablePkCloum = $(this).attr("obj-pk-colum");
		var tablePrimaryKeys =$(this).attr("obj-primary-keys");
		btnMap[tableObjName] = eval({"tableName":tableName,"tableTextName":tableTextName,"tableObjName":tableObjName,"tablePkCloum":tablePkCloum,"tablePrimaryKeys":tablePrimaryKeys});
	});
	var len  = Object.keys(btnMap).length;
	if(len<1){
		layer.alert("请至少选中一个表对象！");
		return;
	}
	parent.preObjTableCallBack(btnMap);
}
</script>   
    
  </div>
  <!-- /.content-wrapper -->
</div>  

</body>
	


</html>

